home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8466 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  110 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!friend!news
  3. From: rich@kastle.com (Richard Krehbiel)
  4. Subject: Re: realloc(NULL,100)
  5. Message-ID: <1996Mar4.122207.16986@friend.kastle.com>
  6. Sender: news@friend.kastle.com (News)
  7. Reply-To: rich@kastle.com
  8. Organization: Kastle Development Associates
  9. X-Newsreader: Forte Free Agent 1.0.82
  10. References: <31346CB0.41C67EA6@jupiter.di.uminho.pt> <TANMOY.96Feb28151825@qcd.lanl.gov>
  11. Date: Mon, 4 Mar 1996 12:23:15 GMT
  12.  
  13. tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) wrote:
  14.  
  15. >Except for the undefined behaviour, returning null or a valid address
  16. >are both correct behaviours. Crashing is not.
  17.  
  18. Let me just offer a "corrected" program (hopefully free of standards
  19. violations):
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. int main(void)
  25. {
  26.    void *p = realloc(NULL, 100);
  27.    printf("%p\n", p);
  28.    return 0;
  29. }
  30.  
  31. The original poster said that he got NULL under some environments.
  32. Several replies pointed out that it is legal and valid for this small
  33. program to indicate a NULL result.  Sure, it's a valid result, any
  34. malloc() call may fail.  But I would be shocked to hear that I should
  35. reasonably expect the above to show NULL.  The hosted environment that
  36. does so is either broken or limited unto uselessness.
  37.  
  38. I'm sure the standard says nothing (and it can say nothing) about
  39. exactly how much dynamic memory might be available to an executing
  40. program.  Perhaps the answer to "how much memory is available" is
  41. different from instant to instant, for reasons not under the program's
  42. control, due to the environment serving memory resources to several
  43. concurrently executing programs.  So from a strictly standards-
  44. guaranteed standpoint, the more robust program should avoid malloc
  45. entirely, because there's no guarantee that malloc will ever return
  46. anything besides NULL.  (I have a suspicion that every useful C
  47. program is at least implementation-defined, if not undefined, by
  48. strict interpretation of the standard.)
  49.  
  50. Something else I wonder; is it valid for realloc(NULL, 100) to fail
  51. (return NULL) in circumstances that malloc(100) would not?  Are the
  52. mechanics of memory allocation so implementation-defined that it's not
  53. possible to say?  Hm.  Probably.
  54.  
  55. Tell me how silly the following is:
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59.  
  60. int main(void)
  61. {
  62.    void *p;
  63.    char *source;
  64.  
  65.    if((p = realloc(NULL, 100)) == NULL)
  66.    {
  67.       /* realloc failed, try malloc */
  68.       p = malloc(100);
  69.       source = "malloc";  /* Say the memory came from malloc */
  70.    }
  71.    else
  72.    {
  73.       source = "realloc";  /* Say the memory came from realloc */
  74.    }
  75.  
  76.    if(p)
  77.       printf("Succeed, %s\n", source);
  78.    else
  79.       printf("Fail, %s\n", source);
  80.  
  81.    return 0;
  82. }
  83.  
  84.  
  85. Similarly silly:
  86.  
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89.  
  90. int main(void)
  91. {
  92.    void *p;
  93.    int i;
  94.  
  95.    /* Try really hard, up to 100 times, to get the memory
  96.       I desparately need */
  97.    for(i = 0, p = NULL; p != NULL && i < 100; i++)
  98.       p = malloc(100);
  99.  
  100.    return p ? EXIT_SUCCESS : EXIT_FAILURE;
  101. }
  102.  
  103. Sheesh.  Now I'm fumed.  By the standard's grace, none of my programs
  104. that use malloc may be expected to do a damned thing.
  105.  
  106. --
  107. Richard Krehbiel, Kastle Systems, Arlington VA USA
  108. rich@kastle.com (work) or richk@mnsinc.com (personal)
  109.  
  110.